package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.ydd.dto.PageDto;
import com.itheima.ydd.entity.Result;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.YddUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/4/9 16:35
 */
@RestController
@Slf4j
@RequestMapping("/ydd_user")
public class YddUserController {
    @Autowired
    private YddUserService yddUserService;


    /**
     * 根据id查询用户详情
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResponseEntity<Result> rowInfo(String id){
        YddUser yddUser = yddUserService.findById(id);
        return ResponseEntity.ok(new Result(0,"成功",yddUser));
    }

    /**
     * 分页,条件查询用户列表
     * @param sex
     * @param username
     * @param mobile
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<Result> getList( String sex,String username,String mobile,String page,String limit){
        PageDto<YddUser> iPage=yddUserService.getPage(sex,username,mobile,page,limit);
        return ResponseEntity.ok(new Result(0,"成功",iPage));
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping ("/add")
        public ResponseEntity<Result> add(@RequestBody YddUser user){
        yddUserService.add(user);
        return ResponseEntity.ok(new Result(0,"成功"));
    }

    /**
     * 用户删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResponseEntity<Result> delete(String id){
        yddUserService.delete(id);
        return ResponseEntity.ok(new Result(0,"成功"));
    }

    /**
     * 修改会员
     * @param user
     * @return
     */
    @PostMapping ("/update")
    public ResponseEntity<Result> update(@RequestBody YddUser user){
        yddUserService.edit(user);
        return ResponseEntity.ok(new Result(0,"成功"));
    }
}
